﻿using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;
using Foundation;
using Microsoft.Win32;
using System.Linq;
using Poet.Views;

namespace Poet.ViewModels
{
    /// <summary>
    /// Main view model.
    /// </summary>
    public class MainViewModel : ViewModel
    {   
        private readonly OpenFileDialog _openFileDialog = new OpenFileDialog();

        public MainViewModel()
        {
            ShowStandartToolBar = true;
            DocumentViews = new ObservableCollection<DocumentView>();
            About = new Command(o => new AboutView { DataContext = new AboutViewModel() }.ShowDialog());
            Exit = new Command(o => Application.Current.MainWindow.Close());
            CloseAll = new RoutedCommand(PropertyNameProvider.GetPropertyName(() => SaveAll), GetType());
            SaveAll = new RoutedCommand(PropertyNameProvider.GetPropertyName(() => SaveAll), GetType());

            CommandBindings.Add(new CommandBinding(ApplicationCommands.New, NewExecuted));
            CommandBindings.Add(new CommandBinding(ApplicationCommands.Open, OpenExecuted));
            CommandBindings.Add(new CommandBinding(SaveAll, SaveAllExecuted, SaveAllCanExecute));
            CommandBindings.Add(new CommandBinding(CloseAll, CloseAllExecuted, CloseAllCanExecute));
        }

        public ICommand Exit { get; set; }

        public ICommand About { get; set; }

        public ICommand SaveAll { get; set; }

        public ICommand CloseAll { get; set; }

        public bool ShowStandartToolBar
        {
            get
            {
                return Get(() => ShowStandartToolBar);
            }
            set
            {
                Set(() => ShowStandartToolBar, value);
            }
        }

        public ObservableCollection<DocumentView> DocumentViews { get; set; }

        private void NewExecuted(object sender, ExecutedRoutedEventArgs args)
        {
            CreateNewDocument();
        }

        private void OpenExecuted(object sender, ExecutedRoutedEventArgs args)
        {
            if (_openFileDialog.ShowDialog() != true)
            {
                return;
            }

            foreach (var fileStream in _openFileDialog.OpenFiles())
            {
                var documentViewModel = CreateNewDocument();
                documentViewModel.Load(fileStream);
            }
        }

        private void SaveAllCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = DocumentViews.Any(v => ApplicationCommands.Save.CanExecute(null, v));
        }

        private void SaveAllExecuted(object sender, ExecutedRoutedEventArgs executedRoutedEventArgs)
        {
            foreach (var documentView in DocumentViews.Where(v => ApplicationCommands.Save.CanExecute(null, v)))
            {
                ApplicationCommands.Save.Execute(null, documentView);
            }
        }

        private void CloseAllCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = DocumentViews.Any(v => ApplicationCommands.Close.CanExecute(null, v));
        }

        private void CloseAllExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            foreach (var documentView in DocumentViews.Where(v => ApplicationCommands.Close.CanExecute(null, v)).ToList())
            {
                ApplicationCommands.Close.Execute(null, documentView);
            }
        }

        private DocumentViewModel CreateNewDocument()
        {
            var view = new DocumentView();
            var viewModel = new DocumentViewModel();
            viewModel.Bind(view);
            //viewModel.OnClosed += (o, e) => view.Close();
            DocumentViews.Add(view);
            return viewModel;
        }
    }
}
